/*
 * Copyright 2008 Federal Emergency Management Agency
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0 
 *    
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 *  
 */
package gov.fema.hazus.bdc.client.hazards.wind.manufacturedhousing;

import gov.fema.hazus.bdc.client.common.ui.AbstractForm;
import gov.fema.hazus.bdc.client.common.ui.BuildingUpdate;
import gov.fema.hazus.bdc.client.common.ui.GenericAnswerListBox;
import gov.fema.hazus.bdc.client.hazards.wind.WindConstants;
import gov.fema.hazus.bdc.shared.entity.Building;
import gov.fema.hazus.bdc.shared.entity.ManufacturedHousing;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;

public class ManufacturedHousingForm extends AbstractForm implements BuildingUpdate {

	private Label tieDowns;
	private Label housingCode;
	private Label windZone;
	
	// form fields
	private GenericAnswerListBox formTieDowns = new GenericAnswerListBox();
	private WindMfgHousingCodeListBox formHousingCode = new WindMfgHousingCodeListBox();
	private WindMfgHousingWindZoneListBox formWindZone = new WindMfgHousingWindZoneListBox();
	
	// ui components
	private DockPanel docker;
	private Grid table = new Grid(3,2);	
	
	// entities
	private ManufacturedHousing manufacturedHousing;
	
	// listeners
	WindManufacturedHousingListBoxChangeListener windManufacturedHousingListBoxChangeListener;
	private WindConstants constants;
	
	public ManufacturedHousingForm(Building building)
	{
		// do parent
		super(building);
		
		// set up widgets
		docker = new DockPanel();
		
		// set up tab title
		constants = (WindConstants)GWT.create(WindConstants.class);
		tabTitle = constants.windManufacturedHousingTabTitle();
		
		tieDowns = new Label(constants.windManufacturedHousingTieDowns());
		housingCode = new Label(constants.windManufacturedHousingHousingCode());
		windZone = new Label(constants.windManufacturedHousingWindZone());
		
		// add labels to form
		table.setWidget(0, 0, tieDowns);
		table.setWidget(1, 0, housingCode);
		table.setWidget(2, 0, windZone);
		
		// set up form fields
		manufacturedHousing = building.getManufacturedHousing();
		windManufacturedHousingListBoxChangeListener = new WindManufacturedHousingListBoxChangeListener(manufacturedHousing);
		
		// listeners
		loadListeners();
		
		// forms
		loadForms();
		
		// add fields to table
		table.setWidget(0, 1, formTieDowns);
		table.setWidget(1, 1, formHousingCode);
		table.setWidget(2, 1, formWindZone);
		
		// add table to docker
		docker.add(table, DockPanel.CENTER);
		
		initWidget(docker);			
		
	}

	public void updateBuilding(Building building) {
		
		// entities
		setBuilding(building);
		this.manufacturedHousing = building.getManufacturedHousing();
		
		// listeners
		windManufacturedHousingListBoxChangeListener.updateManufacturedHousing(building.getManufacturedHousing());
		
		// reload forms
		loadForms();
	}
	
	private void loadForms()
	{
		if( manufacturedHousing != null)
		{
			formTieDowns.setListItem( manufacturedHousing.getTieDowns() );
		
			formTieDowns.setName(ManufacturedHousing.TIEDOWNS);
			
			formHousingCode.setListItem( manufacturedHousing.getHousingCode() );
			formHousingCode.setName(ManufacturedHousing.HOUSINGCODE);
			
			formWindZone.setListItem( manufacturedHousing.getWindZone() );
			formWindZone.setName(ManufacturedHousing.WINDZONE);
		}
		
		if( building != null)
		{
	        if( building.getMode().equalsIgnoreCase(Building.READ_ONLY))
	        {
	            formTieDowns.setEnabled(false);
	            formHousingCode.setEnabled(false);
	            formWindZone.setEnabled(false);
	        }
	        else
	        {
	            formTieDowns.setEnabled(true);
	            formHousingCode.setEnabled(true);
	            formWindZone.setEnabled(true);            
	        }
		}
        
	}
	
	private void loadListeners()
	{
		formTieDowns.addChangeListener(windManufacturedHousingListBoxChangeListener);
		formHousingCode.addChangeListener(windManufacturedHousingListBoxChangeListener);
		formWindZone.addChangeListener(windManufacturedHousingListBoxChangeListener);
	}
}
